class Uv < Formula
  desc "Extremely fast Python package installer and resolver, written in Rust"
  homepage "https://docs.astral.sh/uv/"
  url "https://github.com/astral-sh/uv/archive/refs/tags/0.9.10.tar.gz"
  sha256 "85608865b49c4dbf7205cd3dc9d91856efdb59a6703ffa1b0aab2a4d729b6735"
  license any_of: ["Apache-2.0", "MIT"]
  head "https://github.com/astral-sh/uv.git", branch: "main"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_tahoe:   "a9d65821a147fce0599b4450479832ffe88a2275afef27ca6183ec6d558154bc"
    sha256 cellar: :any_skip_relocation, arm64_sequoia: "6cb0db831e5e1d88dee4f28fde3cfa569b610e231225a1d43aa2cb3e43348b8d"
    sha256 cellar: :any_skip_relocation, arm64_sonoma:  "df3bdc92095df71dff79af834eec4beccc539d56225fc6dcada4680202fffd95"
    sha256 cellar: :any_skip_relocation, sonoma:        "d3f1d2a3d667e1d913aadf950c2e29eafe4916e51d7d48600db550b5baf3be8f"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "4aa27b992e4d6b9ae44586f609d7f3568ef685f583a48e8e1340f50071bf50e1"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "54d4e24b103e67a86cab274ecb51712b8cea93827aa6cef5bc815923d767bc5a"
  end

  depends_on "pkgconf" => :build
  depends_on "rust" => :build

  uses_from_macos "python" => :test
  uses_from_macos "bzip2"
  uses_from_macos "xz"

  def install
    ENV["UV_COMMIT_HASH"] = ENV["UV_COMMIT_SHORT_HASH"] = tap.user
    ENV["UV_COMMIT_DATE"] = time.strftime("%F")
    # See: https://github.com/astral-sh/uv/issues/15401
    ENV["JEMALLOC_SYS_WITH_LG_PAGE"] = "16" if Hardware::CPU.arm? && OS.linux?
    system "cargo", "install", *std_cargo_args(path: "crates/uv")
    generate_completions_from_executable(bin/"uv", "generate-shell-completion")
    generate_completions_from_executable(bin/"uvx", "--generate-shell-completion")
  end

  test do
    (testpath/"requirements.in").write <<~REQUIREMENTS
      requests
    REQUIREMENTS

    compiled = shell_output("#{bin}/uv pip compile -q requirements.in")
    assert_match "This file was autogenerated by uv", compiled
    assert_match "# via requests", compiled

    assert_match "ruff 0.5.1", shell_output("#{bin}/uvx -q ruff@0.5.1 --version")
  end
end
